import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:study_online/pages/studyPage/StudyModel.dart';
import 'package:study_online/entity/Category.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/screen_util.dart';
import 'package:study_online/pages/chatPage/main.dart';
import 'package:study_online/global/colors.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      builder: EasyLoading.init(),
      home: Scaffold(
        //appBar: AppBar(
        //  backgroundColor: Colors.black26,
        //  title: Text('container'),
        //),
        body: CourseCatalogueTab(),
      ),
    );
  }
}
class CourseCatalogueTab extends StatefulWidget {
  CourseCatalogueTab({Key key}) : super(key: key);

  @override
  _CourseCatalogueTabState createState() {
    return _CourseCatalogueTabState();
  }
}

class _CourseCatalogueTabState extends State<CourseCatalogueTab> with AutomaticKeepAliveClientMixin{
  CategoryList _categoryList=new CategoryList([]);
  var list = {
    '第一章,集合与函数': ['1.1集合', '1.2函数', '1.3集合与函数'],
    '第二章,导数': ['2.1导', '2.2数', '2.3导', '2.4数', '2.5导', '2.6数'],
    '第三章,圆锥曲线': ['3.1圆', '3.2锥', '3.3曲', '3.4线'],
    '第四章,空间集合': ['4.1空', '4.2间', '4.3几', '4.4何'],
    '第五章,向量': ['5.1向', '5.2量'],
    '第六章,解三角形': ['6.1解', '6.2三', '6.3角', '6.4形'],
  };
   _loadData() async {
     Response response=await getCategoryByCouId(1);
    print(response.toString());
    _categoryList=CategoryList.formJson(json.decode(response.toString()));
    //List responseData=json.decode(response.toString());
    setState(() {
      //_list=response;
      _categoryList=CategoryList.formJson(json.decode(response.toString()));
    });
  }
  @override
  void initState() {
    super.initState();
    _loadData();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(
      BoxConstraints(
          maxWidth: MediaQuery.of(context).size.width,
          maxHeight: MediaQuery.of(context).size.height),
      designSize: Size(1080, 2340),
    );
    if(_categoryList==null)
      return Container(
        height: 0,
        width: 0,
      );
    else
    return Container(
      child: ListView(
        children: _buildList(),
      ),
    );
  }

  List<Widget> _buildList() {
    List<Widget> widgets = [];
    // _list.forEach((key) {
    //   widgets.add(_item(key, list[key]));
    // });
    for(int i=0;i<_categoryList.data.length;++i){
      widgets.add(_item(_categoryList.data[i].titile.substring(1),_categoryList.data[i].subTitle));
    }
    return widgets;
  }

  Widget _titleWidget(value) {
    return Row(
      children: [
        Container(
          height: ScreenUtil().setHeight(47),
          width: ScreenUtil().setWidth(8),
          decoration: BoxDecoration(
            color: AppColors.primary,
            borderRadius:
            BorderRadius.all(Radius.circular(ScreenUtil().setWidth(4))),
          ),
        ),
        SizedBox(
          width: ScreenUtil().setWidth(22),
        ),
        Text(
          value,
          style: TextStyle(
            color: AppColors.primary,
            fontWeight: FontWeight.bold,
            fontSize: ScreenUtil().setSp(50),
          ),
        )
      ],
    );
  }

  Widget _item(String title, List<String> subTitle) {
    return Container(
      width: ScreenUtil().screenWidth-ScreenUtil().setWidth(110),
      margin: EdgeInsets.fromLTRB(
        ScreenUtil().setWidth(55),
        ScreenUtil().setHeight(47),
        ScreenUtil().setWidth(55),
        ScreenUtil().setHeight(0),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _titleWidget(title),
          Container(
            width: ScreenUtil().screenWidth-ScreenUtil().setWidth(110),
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(50)),
            decoration: BoxDecoration(
              color: Color(0x79edeeef),
              borderRadius: BorderRadius.all(Radius.circular(7)),
            ),
            child: ListView.builder(
              scrollDirection: Axis.vertical,
              itemCount: subTitle.length,
              shrinkWrap: true,
              itemBuilder: (context,index){
                return
                  Container(
                    height: ScreenUtil().setHeight(144),
                    width: ScreenUtil().screenWidth-ScreenUtil().setWidth(110),
                    padding: EdgeInsets.fromLTRB(
                      ScreenUtil().setWidth(25),
                      ScreenUtil().setHeight(35),
                      ScreenUtil().setWidth(25),
                      ScreenUtil().setHeight(0),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text(
                          subTitle[index].toString(),
                          style:TextStyle(
                            fontSize: ScreenUtil().setSp(46),
                            color:Colors.black,
                          ),
                        ),
                        Divider(height: ScreenUtil().setHeight(8),color: AppColors.subtext,)
                      ],
                    ),
                  );
              },
            ),
          ),
        ],
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}

